package com.appspot.authorizationdemo.server.authorization;

import java.util.ArrayList;

import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.appspot.authorizationdemo.shared.UserRoleType;


/**
 * 
 * @author alex.jongman
 * This class is meant to run on the Google App Engine and 
 * requires the Google Datastore.
 * This class is used by the class Authorization and uses the 
 * enumeration class UserRoleType
 * 
 */
@PersistenceCapable
public class AuthorizedUser {

	@PrimaryKey
	private String email;
	@Persistent
	private ArrayList<UserRoleType> belongsTo;

	/**
	 * Instances a new User
	 * 
	 * @param email
	 *            Email address of the system user
	 * @param belongsTo
	 *            groups assigned to the user
	 */
	public AuthorizedUser(String email, ArrayList<UserRoleType> belongsTo) {
		this.email = email;
		this.belongsTo = belongsTo;
	}

	/**
	 * Fetch the users email address
	 * 
	 * @return the users email address 
	 */
	public String getEmail() {
		return this.email;
	}

	/**
	 * Fetch the lists of roles assigned to the user
	 * 
	 * @return Roles assigned to the user
	 */
	public ArrayList<UserRoleType> getBelongsTo() {
		return this.belongsTo;
	}

	/**
	 * Adds a right
	 * 
	 * @param urt
	 *            right to be added
	 * @return true
	 */
	public boolean addRight(UserRoleType urt) {
		boolean result = true;
		if (!this.belongsTo.contains(urt)) {
			result = this.belongsTo.add(urt);
		}
		return result;
	}

	/**
	 * Removes a right
	 * 
	 * @param urt
	 *            right to be removed
	 * @return true if the right was in the list
	 */
	public boolean removeRight(UserRoleType urt) {
		boolean result = true;

		result = this.belongsTo.remove(urt);
		
		return result;
	}

}
